package cn.cxq.learning.array;

import java.util.HashMap;

public class MoreThanHalfNum {

    public int MoreThanHalfNum_Solution(int[] array) {

        int halfLength = array.length / 2;

        HashMap<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < array.length; i++) {
            if (map.containsKey(array[i])) {
                map.put(array[i], map.get(array[i]) + 1);
            } else {
                map.put(array[i], 1);
            }
            if (map.get(array[i]) > halfLength) {
                return array[i];
            }
        }

        return 0;
    }
}
